package com.augurit.common.system.web.org;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.system.convert.OmUserPrivConvert;
import com.augurit.common.system.service.org.IAcApplicationService;
import com.augurit.common.system.service.org.IAcFunctionService;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IAcServerService;
import com.augurit.common.system.util.PrivilegeUtils;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.system.web.org.form.OmUserPrivForm;

/**
 * 机构、岗位、用户权限树Action类
 */
@SuppressWarnings("serial")
public class PrivilegeAction extends ExtCrudActionSupport<OmUserPrivForm> {

	// Service 引用
	@Autowired
	private IAcServerService acServerService;
	
	@Autowired
	private IAcApplicationService acApplicationService;
	
	@Autowired
	private IAcRoleService acRoleService;
	
	@Autowired
	private IAcFunctionService acFunctionService;

	// 基本属性
	private OmUserPrivForm form;
	private Page<AcRoleForm> page = new Page<AcRoleForm>(15);
	
	private Long orgId;
	private Long groupId;
	private Long posId;
	private Long userId;
	
	private Long roleId;
	private Long[] roleIds;
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 获取机构已经授权的角色列表或角色功能列表
	 */
	public String getPrivilegeInfoByOrg()throws Exception{
		//获取指定机构的已授权角色列表
		if(PrivilegeUtils.isAuthedNode(form)){
			this.getRolesByOrg();
		}
		//获取指定角色的功能列表
		else{
			this.getFuncsByRoleId(form.getRoleId());
		}

		return null;
	}
	
	/**
	 * 根据机构ID获取已授权角色列表
	 */
	public String getRolesByOrg() throws Exception{
		List<AcRoleForm> list = acRoleService.getRolesByOrgId(orgId);
		renderJson(OmUserPrivConvert.convertRolesToUserPrivForms(list, true));
		return null;
	}
	
	/**
	 * 根据角色ID获取功能对象列表
	 */
	public String getFuncsByRoleId(Long roleId) throws Exception{
		List<AcFunctionForm> list = acRoleService.getFuncsByRoleId(roleId, 
				WebContextUtils.getAcApplicationMap(
						acServerService, acApplicationService, this.getApplication()));
		
		if(list != null && list.size() > 0){
			renderJson(OmUserPrivConvert.convertFunsToUserPrivForms(list, null));
		}
		return null;
	}
	
	/**
	 * 删除已经授权的机构角色
	 */
	public String deleteRoleFormOrg()throws Exception{
		acRoleService.disAssociateRoleFromOrg(roleId, orgId);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 获取指定机构的未授权角色
	 */
	public String getUnAuthedRolesWithOrg()throws Exception {
		acRoleService.getRolesUnAssociateOrg(orgId, page);
		this.extRenderGridJson(page);
		return null;
	}
	

	/**
	 * 给机构授权新角色
	 */
	public String addRolesToOrg()throws Exception{
		if(orgId != null && roleIds != null && roleIds.length > 0){
			for(Long roleId : roleIds)
				acRoleService.assignRoleToOrg(roleId, orgId);
			
			this.extRenderSuccess();
		}
		return null;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 获取工作组已经授权的角色列表或角色功能列表
	 */
	public String getPrivilegeInfoByGroup()throws Exception{
		//获取指定机构的已授权角色列表
		if(PrivilegeUtils.isAuthedNode(form)){
			this.getRolesByGroup();
		}
		//获取指定角色的功能列表
		else{
			this.getFuncsByRoleId(form.getRoleId());
		}
		return null;
	}
	
	/**
	 * 根据工作组ID获取已授权角色列表
	 */
	public String getRolesByGroup() throws Exception{
		List<AcRoleForm> list = acRoleService.getRolesByGroupId(groupId);
		renderJson(OmUserPrivConvert.convertRolesToUserPrivForms(list, true));
		return null;
	}
	
	/**
	 * 获取指定工作组的未授权角色
	 */
	public String getUnAuthedRolesWithGroup()throws Exception {
		acRoleService.getRolesUnAssociateGroup(groupId, page);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 给工作组授权新角色
	 */
	public String addRolesToGroup()throws Exception{
		if(groupId != null && roleIds != null && roleIds.length > 0){
			for(Long roleId : roleIds)
				acRoleService.assignRoleToGroup(roleId, groupId);
			
			this.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 删除已经授权的工作组角色
	 */
	public String deleteRoleFromGroup()throws Exception{
		acRoleService.disAssociateRoleFromGroup(roleId, groupId);
		this.extRenderSuccess();
		return null;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	public String getPrivilegeInfoByPos() throws Exception{
		//获取指定岗位的已授权角色列表
		if(PrivilegeUtils.isAuthedNode(form)){
			this.getRolesByPos();
		}
		//获取指定角色的功能列表
		else{
			this.getFuncsByRoleId(form.getRoleId());
		}
		return null;
	}

	/**
	 * 根据岗位ID获取授权角色
	 */
	public String getRolesByPos() throws Exception{
		List<AcRoleForm> list = acRoleService.getRolesByPosId(posId);
		renderJson(OmUserPrivConvert.convertRolesToUserPrivForms(list, true));
		return null;
	}

	/**
	 * 删除已经授权的岗位角色
	 */
	public String deleteRoleFromPos()throws Exception{
		acRoleService.disAssociateRoleFromPos(roleId, posId);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 获取指定岗位的未授权角色
	 */
	public String getUnAuthedRolesWithPos()throws Exception {
		acRoleService.getRolesUnAssociatePos(posId, page);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 给岗位授权新角色
	 */
	public String addRolesToPos()throws Exception{
		if(posId != null && roleIds!= null && roleIds.length>0){
			for(Long roleId:roleIds)
				acRoleService.assignRoleToPosition(roleId, posId);
			this.extRenderSuccess();
		}
		return null;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 获取用户权限数据
	 */
	public void getPrivilegeInfoByUser() throws Exception {
		// 获取指定用户的已授权角色
		if (PrivilegeUtils.isAuthedNode(form)){
			this.getRolesByUser();
		}
		// 获取指定用户特别禁止的功能
		else if(PrivilegeUtils.isForbidNode(form)){
			this.getForbiddenFuns();
		}
		// 获取指定用户特别开通的功能
		else if(PrivilegeUtils.isPermitNode(form)){
			this.getPermittedFuns();
		}
		// 获取指定用户的继承角色
		else if(PrivilegeUtils.isInheritNode(form)){
			this.getExtendedRoles();
		}
		// 根据角色ID获取功能
		else{
			getFuncsByRoleId(form.getRoleId());
		}
	}
	
	/**
	 * 获取指定用户的已授权角色，并转化为Ext节点
	 */
	public String getRolesByUser() throws Exception {
		//根据用户ID获取直属角色列表
		List<AcRoleForm> list = acRoleService.getDirectRolesByUserId(userId);
		renderJson(OmUserPrivConvert.convertRolesToUserPrivForms(list, true));
		return null;
	}
	
	/**
	 * 获取指定用户特别禁止的功能
	 */
	public String getForbiddenFuns() throws Exception {
		//根据用户ID获取特别禁止功能集合
		List<AcFunctionForm> list = acFunctionService.getForbidenFuncsByUserId(userId,
				WebContextUtils.getAcApplicationMap(
						acServerService, acApplicationService, this.getApplication()));
		renderJson(OmUserPrivConvert.convertFunsToUserPrivForms(list, PrivilegeUtils.FORBID_NODE));
		return null;
	}

	/**
	 * 获取指定用户特别开通的功能
	 */
	public String getPermittedFuns() throws Exception {
		//根据用户ID获取特别开通功能集合
		List<AcFunctionForm> list = acFunctionService.getPermittedFuncsByUserId(userId,
				WebContextUtils.getAcApplicationMap(
						acServerService, acApplicationService, this.getApplication()));
		renderJson(OmUserPrivConvert.convertFunsToUserPrivForms(list, PrivilegeUtils.PERMIT_NODE));
		return null;
	}
	
	/**
	 * 获取指定用户的继承角色
	 */
	public String getExtendedRoles() throws Exception {
		//根据用户ID获取继承角色列表
		List<AcRoleForm> list = acRoleService.getExtendedRolesByUserId(userId);
		renderJson(OmUserPrivConvert.convertRolesToUserPrivForms(list, false));
		return null;
	}
	
	/**
	 * 删除用户的已授权角色
	 */
	public String deleteRolesFromUser() throws Exception {
		acRoleService.disAssociateRoleFromUser(roleId, userId);
		this.extRenderSuccess();
		return null;
	}

	/**
	 * 获取指定用户的未授权角色
	 * 注意：前提是当前用户所拥有的角色范围内进行授权
	 */
	public String getUnAuthedRolesWithUser() throws Exception {
		acRoleService.getRolesNotAssociateUser(page, this.getLoginUserId(), userId);
		this.extRenderGridJson(page);
		return null;
	}

	/**
	 * 增加授权角色
	 */
	public String addRolesToUser() throws Exception {
		if (userId != null && roleIds != null && roleIds.length > 0) {
			for (Long roleId : roleIds)
				acRoleService.assignRoleToUser(roleId, userId);
			this.extRenderSuccess();
		}
		return null;
	}
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public OmUserPrivForm getModel(){
		return form;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		form = new OmUserPrivForm();
	}


	// 属性getter和setter方法
	public OmUserPrivForm getForm() {
		return form;
	}

	public void setForm(OmUserPrivForm form) {
		this.form = form;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}

	public Long getPosId() {
		return posId;
	}

	public void setPosId(Long posId) {
		this.posId = posId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}
}
